﻿using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.Dz_ConvertBill.GFT
{
    [Description("采购申请到采购订单按照交货日期对应交货安排数量合并处理")]
    public class ReqToOrderService : AbstractConvertPlugIn
    {
        DynamicObjectCollection sourceObjCollection = null; // 源单单据体数据包
        //获取源单数据       
        public override void OnGetSourceData(GetSourceDataEventArgs e)
        {
            base.OnGetSourceData(e);
            // 源单单据体数据包
            sourceObjCollection = e.SourceData;
        }
        public override void AfterConvert(AfterConvertEventArgs e)
        {
            base.AfterConvert(e);

            //目标单数据
            ExtendedDataEntity[] entrys = e.Result.FindByEntityKey("FPOOrderEntry");
            if (entrys == null)
            {
                return;
            }
            foreach (ExtendedDataEntity entry in entrys)
            {
                decimal qty = 0; //Convert.ToDecimal(entry["qty"]);
                decimal baseQty = 0;// Convert.ToDecimal(entry["BaseUnitQty"]);
                DynamicObjectCollection deliPlan = entry["POOrderEntryDeliPlan"] as DynamicObjectCollection;//子项分录 （交货明细）

                long deliPlanRowNum = deliPlan != null ? deliPlan.Count() : 0;

                DynamicObject WLBM = entry["MaterialId"] as DynamicObject;
                long FMATERIALID = 0;
                if (WLBM != null) FMATERIALID = Convert.ToInt64(WLBM[0].ToString());

                for (int i = 0; i < deliPlanRowNum; i++)
                {
                    qty = 0;
                    baseQty = 0;
                    DateTime PlanDeliveryDate = Convert.ToDateTime(deliPlan[i]["PlanDeliveryDate"]);
                    //查找物料编码 交货日期相同的源单 累计数
                    if (sourceObjCollection.Count > 0)
                    {
                        foreach (var entityData in sourceObjCollection)
                        {
                            long FSQMATERIALID = Convert.ToInt64(entityData["FMaterialId"]);

                            DateTime ArrivalDate = Convert.ToDateTime(entityData["FArrivalDate"]);
                            if (FSQMATERIALID == FMATERIALID && ArrivalDate == PlanDeliveryDate)
                            {
                                qty += Convert.ToDecimal(entityData["FApproveQty"]) - Convert.ToDecimal(entityData["FOrderJoinQty"]);
                                baseQty += Convert.ToDecimal(entityData["FBaseUnitQty"]) - Convert.ToDecimal(entityData["FOrderJNBaseQty"]);
                            }
                        }
                    }
                    deliPlan[i]["PlanQty"] = qty;
                    deliPlan[i]["DeliRemainQty"] = qty;
                    deliPlan[i]["BasePlanQty"] = baseQty;
                    deliPlan[i]["BaseDeliRemainQty"] = baseQty;
                }
            }
        }
    }
}
